﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SyncSocketModuleCore.SyncSocketModule
{
    public class PlcUtil
    {
        public PlcUtil()
        {
        }

        public static int GetBitMask(int _Word, int _BitNum)
        {
            if ((short)(_Word >> (_BitNum & 31) & 1) == 1)
            {
                return 1;
            }
            return 0;
        }

        public static int Int2ToInt(int[] _Int2)
        {
            byte[] numArray = new byte[4];
            byte[] bytes = BitConverter.GetBytes(_Int2[0]);
            byte[] bytes1 = BitConverter.GetBytes(_Int2[1]);
            Array.Copy(bytes, 0, numArray, 0, 2);
            Array.Copy(bytes1, 0, numArray, 2, 2);
            return BitConverter.ToInt32(numArray, 0);
        }

        public static int[] IntToInt2(int _Int)
        {
            byte[] bytes = BitConverter.GetBytes(_Int);
            byte[] numArray = new byte[4];
            byte[] numArray1 = new byte[4];
            Array.Copy(bytes, 0, numArray, 0, 2);
            Array.Copy(bytes, 2, numArray1, 0, 2);
            int[] num = new int[] { BitConverter.ToInt32(numArray, 0), BitConverter.ToInt32(numArray1, 0) };
            return num;
        }

        public static int SetBit2Mask(ref int[] Data, int Index, int BitValue)
        {
            if (Index < 0 || Index > 5)
            {
                return -1;
            }
            int num = 0;
            int data = Data[num];
            switch (Index)
            {
                case 1:
                    {
                        data = data & 65280 | BitValue & 255;
                        break;
                    }
                case 2:
                    {
                        data = data & 255 | BitValue << 8 & 65280;
                        break;
                    }
                default:
                    {
                        return -1;
                    }
            }
            Data[num] = data;
            return num;
        }

        public static int SetBit4Mask(ref int[] Data, int Index, int BitValue)
        {
            if (Index < 0 || Index > 5)
            {
                return -1;
            }
            int num = 0;
            int data = Data[num];
            switch (Index)
            {
                case 1:
                    {
                        data = data & 65520 | BitValue & 15;
                        break;
                    }
                case 2:
                    {
                        data = data & 65295 | BitValue << 4 & 240;
                        break;
                    }
                case 3:
                    {
                        data = data & 61695 | BitValue << 8 & 3840;
                        break;
                    }
                case 4:
                    {
                        data = data & 4095 | BitValue << 12 & 61440;
                        break;
                    }
                default:
                    {
                        return -1;
                    }
            }
            Data[num] = data;
            return num;
        }

        public static int SetBitMask(ref int[] Data, int Index, int BitValue)
        {
            if (Index < 0 || Index > 15)
            {
                return -1;
            }
            int index = Index / 16;
            int num = Index % 16;
            int data = Data[index];
            data = (BitValue != 1 ? data - (1 << (num & 31) & 65535) : data ^ BitValue << (num & 31) & 65535);
            if (data < 0)
            {
                data = 0;
            }
            Data[index] = data;
            return index;
        }

        public static int[] StringToInt(string str, int length)
        {
            int[] numArray = new int[length];
            for (int i = 0; i < (int)numArray.Length; i++)
            {
                numArray[i] = 8224;
            }
            int num = str.Length;
            if (num > 20)
            {
                num = 20;
            }
            if (num % 2 == 1)
            {
                str = string.Concat(str, " ");
                num++;
            }
            int num1 = num / 2;
            for (int j = 0; j < num1; j++)
            {
                numArray[j] = (short)str[2 * j];
                numArray[j] += (short)(str[2 * j + 1] << '\b');
            }
            return numArray;
        }
    }
}
